package wiinf;

import java.awt.event.ActionListener;

/**
 *
 * @author David Roos
 */
public class afdBewerken extends javax.swing.JPanel {
    private serverViewModel model;

    /**
     * Aanmaken nieuwe panel voor afdelingen bewerken
     *
     * @param model De JFrame waar de panel in komt
     */
    public afdBewerken(serverViewModel model) {
        this.model = model;
        initComponents();
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CafdBewerken = new javax.swing.JComboBox();
        TafdBewerken = new javax.swing.JTextField();
        BafdBewerken = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(435, 303));

        model.setAfdelingBox(CafdBewerken);
        CafdBewerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CafdBewerkenActionPerformed(evt);
            }
        });

        BafdBewerken.setText("Opslaan");
        BafdBewerken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BafdBewerkenActionPerformed(evt);
            }
        });

        jLabel1.setText("Nieuwe Naam:");

        jLabel2.setText("Afdeling:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(144, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(TafdBewerken, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CafdBewerken, javax.swing.GroupLayout.Alignment.LEADING, 0, 194, Short.MAX_VALUE)))
                    .addComponent(BafdBewerken, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CafdBewerken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TafdBewerken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(34, 34, 34)
                .addComponent(BafdBewerken)
                .addContainerGap(137, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Actionlistener voor combobox gevuld met afdelingen
     *
     * @param evt action event
     */
    private void CafdBewerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CafdBewerkenActionPerformed
        String item = CafdBewerken.getSelectedItem().toString();
        TafdBewerken.setText(item);
    }//GEN-LAST:event_CafdBewerkenActionPerformed

    /**
     * Action listener voor de opslaan button
     *
     * @param evt action event
     */
    private void BafdBewerkenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BafdBewerkenActionPerformed
        String item = CafdBewerken.getSelectedItem().toString();
        String naam = TafdBewerken.getText();
        model.server.updateMap(item, naam);
        TafdBewerken.setText("");

        ActionListener[] listeners = CafdBewerken.getActionListeners();
        for (ActionListener l : listeners) {
            CafdBewerken.removeActionListener(l);
        }
        CafdBewerken.removeAllItems();
        model.setAfdelingBox(CafdBewerken);
        for (ActionListener l : listeners) {
            CafdBewerken.addActionListener(l);
        }
    }//GEN-LAST:event_BafdBewerkenActionPerformed

    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BafdBewerken;
    private javax.swing.JComboBox CafdBewerken;
    private javax.swing.JTextField TafdBewerken;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

}
